// File: src/bbs/comment.rs
use chrono::Utc;
//use serde::{Serialize, Deserialize};
use sha2::{Sha256, Digest};

//#[derive(Debug, Clone)]//, Serialize, Deserialize)]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct BbsComment {
    //pub id: String,
    //pub post_id: String,
    pub post_hash: String,  // 使用帖子哈希代替ID
    pub content: String,
    pub author: String,
    //#[serde(default = "default_timestamp")]
    pub url: String, // 新增字段
    pub content_hash: Vec<u8>,
    pub timestamp: i64,
}

impl BbsComment {
    pub fn new(post_hash: String, //post_id: String, 
	content: String, author: String) -> Self {
    //pub fn new() -> Self {

        let content_hash = {
            let mut hasher = Sha256::new();
	    hasher.update(""); // 初始空url
            hasher.update(&content);
            hasher.finalize().to_vec()
        };

        Self {
            //id: format!("COMMENT-{}", Utc::now().timestamp_nanos_opt().unwrap()),
            //post_id: post_id,  // 明确字段与参数对应
	    post_hash,
            content,
            author,
	    url: String::new(), // 初始化url字段
	    content_hash,
            timestamp: Utc::now().timestamp(),
        }
    }

    pub fn with_url(mut self, url: String) -> Self {
        // 更新哈希值
        let mut hasher = Sha256::new();
        hasher.update(url.as_bytes()); // 包含url
        hasher.update(&self.content);
        self.content_hash = hasher.finalize().to_vec();

        self.url = url;
        self
    }
}

//fn default_timestamp() -> i64 {
//    Utc::now().timestamp()
//}
